
package com.chuhay.elemwar;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;

import static com.chuhay.elemwar.ElemWarConstants.*;

import com.threerings.media.sprite.Sprite;


public class PieceSprite extends Sprite {
   // The piece this sprite is associated with
   protected ElemWarObject.Piece _piece;

   // private boolean first = true;

   /**
    * Creates a hex sprite to display the supplied game piece.
    */
   public PieceSprite (ElemWarObject.Piece piece) {
      super(SIZE, SIZE);
      updatePiece(piece);
   }

   /**
    * Called when the piece we are displaying has been updated.
    */
   public void updatePiece (ElemWarObject.Piece piece) {
      // keep track of our piece
      _piece = piece;

      // set our location based on the grid location
      setLocation(_piece.x - CORNERDIST, _piece.y - CORNERDIST);

      // Draw pieces last
      setRenderOrder(20);

      // force a redraw on creation
      invalidate();
   }

   @Override
   // from Sprite
   public void paint (Graphics2D gfx) {
      switch (_piece.pieceType) {
      case PIECE_FIRE:
         // Create point arrays and set polygon's points
         int [] triX = {_piece.x, _piece.x + CORNERDIST - 10, _piece.x - CORNERDIST + 10};
         int [] triY = {_piece.y - CORNERDIST + 6, _piece.y + CORNERDIST - 12, _bounds.y + SIZE - 12};
         Polygon s = new Polygon(triX, triY, triX.length);

         // Fill triangle and draw outline
         gfx.setColor(Color.RED);
         gfx.fill(s);
         gfx.setColor(Color.LIGHT_GRAY);
         gfx.draw(s);
         break;
      case PIECE_ICE:
         // draw a filled in circle in our piece color
         gfx.setColor(Color.BLUE);
         gfx.fillOval(_piece.x - CORNERDIST + 6, _piece.y - CORNERDIST + 6, SIZE - 11, SIZE - 11);
         // then outline in grey
         gfx.setColor(Color.LIGHT_GRAY);
         gfx.drawOval(_piece.x - CORNERDIST + 6, _piece.y - CORNERDIST + 6, SIZE - 12, SIZE - 12);
         break;
      case PIECE_EARTH:
         // draw a filled in square in our piece color
         int [] squareX = {_piece.x - CORNERDIST + 8, _piece.x + CORNERDIST - 8};
         int [] squareY = {_piece.y - CORNERDIST + 8, _piece.y + CORNERDIST - 8};
         gfx.setColor(Color.GREEN);
         gfx.fillRect(squareX[0], squareY[0], squareX[1] - squareX[0], squareY[1] - squareY[0]);
         // then outline in grey
         gfx.setColor(Color.LIGHT_GRAY);
         gfx.drawRect(squareX[0], squareY[0], squareX[1] - squareX[0], squareY[1] - squareY[0]);
         break;
      }
   }
}
